﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Soveryn.Commerce.BaseClasses;
using Soveryn.Commerce.Shared;

namespace Soveryn.Commerce.Doodads
{

    public partial class ItemList : ItemListBaseClass
    {

        new protected void Page_Load(object sender, EventArgs e)
        {

            base.Page_Load(sender, e);

            if (IsPostBack)
                return;

            if (!DataUtils.HasRecords(items, 0))
                return;

            ItemRepeater.DataSource = items.Tables[0].DefaultView;
            ItemRepeater.DataBind();

            ItemListHTMLLiteral.Text = Properties.DoodadInstanceHTML;

            ItemListContainer.CssClass = GetPropertyValue("ItemListClass", "itemList");

            if (ParentPage.EditMode && ParentPage.EmployeeLoggedIn)
            {
                HTMLPanel.CssClass = "editable";
                HTMLPanel.Attributes.Add("data-id", Properties.ElementID);
            }

        }

        protected void ItemRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
        {
           
            DataRowView drv = (DataRowView)e.Item.DataItem;
            ItemProperties item = ItemProperties.Create(drv.Row);
           
            HiddenField ItemIDHiddenField = (HiddenField)e.Item.FindControl("ItemIDHiddenField");
            Literal ItemNameLiteral = (Literal)e.Item.FindControl("ItemNameLiteral");
            HyperLink ItemNameHyperLink = (HyperLink)e.Item.FindControl("ItemNameHyperLink");
            HyperLink ItemImageHyperLink = (HyperLink)e.Item.FindControl("ItemImageHyperLink");
            Literal ItemSKUSLiteral = (Literal)e.Item.FindControl("ItemSKUSLiteral");
            Image ItemImage = (Image)e.Item.FindControl("ItemImage");


            Literal ItemSummaryLiteral = (Literal)e.Item.FindControl("ItemSummaryLiteral");
            Panel ItemBoxPanel = (Panel)e.Item.FindControl("ItemBoxPanel");

            //ItemBoxPanel.CssClass = "Item";
            //if (Convert.ToBoolean(GetPropertyValue("doShowItemListCustomClass")) == true && !String.IsNullOrEmpty(item.ItemListCustomClass))
            //   ItemBoxPanel.CssClass += " " + item.ItemListCustomClass;

            if (Convert.ToBoolean(GetPropertyValue("doShowItemSummary")) == false)
                ItemSummaryLiteral.Visible = false;

            ItemIDHiddenField.Value = item.ItemID.Value.ToString();
            ItemNameLiteral.Text = item.ItemName;
            ItemNameHyperLink.NavigateUrl = item.SEO.GetURL(ParentPage);

            ItemImageHyperLink.NavigateUrl = ItemNameHyperLink.NavigateUrl;
            ItemSummaryLiteral.Text = item.ItemSummary;

            if (String.IsNullOrEmpty(item.ItemImageFile))
                item.ItemImageFile = "lostImage.jpg";

            ItemImage.ImageUrl = "/ProductImagesCache/" + GetPropertyValue("ItemList_ImageSize") + "-" + item.ItemImageFile;
            ItemImage.AlternateText = item.ItemImageAlt;
            ItemImage.ToolTip = item.ItemName;

            if (item.ItemDisplaySKUListOnItemList.Value)
            {
                DataRow[] skuRows = drv.Row.GetChildRows("ItemSKU");
                
                foreach (DataRow skuRow in skuRows)
                {
                    ItemSKUProperties sku = ItemSKUProperties.Create(skuRow);
                    if (sku.SKUDisplayStatus == (int)Enums.DisplayRights.Everyone)
                    {
                        String originalPrice = "";
                        if (sku.SKUSalePrice > 0)
                            originalPrice = "<span class='listOriginalPrice'>" + sku.SKUOriginalPrice.Value.ToString("C") + "</span>";
                        
                        ItemSKUSLiteral.Text += "<div class='listSku'><span class='listSkuName'>" + sku.SKUName +  "</span>" + originalPrice + "<span class='price'>" + sku.SKUUnitPrice_Calc.Value.ToString("C") + "</span></div>";
                    }
                }
            }

        }

    }
}
